// card-recharge
const app = getApp();
const { PageBase } = require('../../mixins/page');
const { setPhoneBarColor, platformPay, navTo, tipSuccess, tipError, tipToast } = require('../../common/platform');
const { createRechargeOrder, createRechargePrepay, getMemberAccountInfo, noticeRechargeResult } = require('../../api/member');
const { getMemberCardDiscount } = require('../../api/market');
const { PAGE_NAV } = require('../../constants/route');

Page({
    mixins: [PageBase],
    data: {
        cardInfo: null,
        rechargeList: [{
            key: 1,
            value: 10000,
        }, {
            key: 2,
            value: 20000,
        }, {
            key: 3,
            value: 50000,
        }],
        selectedKey: 1,
        customValue: 0,
    },
    onLoad: function (options) {
        this.setData({
            cardInfo: app.globalData.cardInfo,
        });
    },
    onShow() {
        console.log(this.data.theme);
        // 设置标题栏底色
        this.data.theme && setPhoneBarColor(this.data.theme);
    },

    selectRechargeItem(event) {
        this.setData({
            selectedKey: event.detail,
        });
    },
    async rechargeAction() {
        const { cardInfo } = app.globalData;
        console.log('充值操作', cardInfo);
        const { selectedKey, rechargeList, customValue } = this.data;
        const rechargeAmount = customValue ? customValue * 100 : rechargeList.filter(item => item.key == selectedKey)[0].value;
        console.log('Recharge => 充值金额为：', rechargeAmount, cardInfo.rechargeUp);
        if (rechargeAmount < cardInfo.rechargeUp) {
            tipToast(`最低充值金额为：${cardInfo.rechargeUp / 100}元`);
            return;
        }
        const params = {
            acc_id: cardInfo.accId,
            store_id: 0,
            amount: rechargeAmount,
        };
        const { order_no } = await createRechargeOrder(params);
        const info = await createRechargePrepay(order_no);
        console.log('支付信息', info);
        const prepay = JSON.parse(info.prepay_info);
        // 执行支付操作
        const rechargeFalg = await platformPay(prepay);
        rechargeFalg ? tipSuccess('充值成功') : tipError('充值失败');
        rechargeFalg && await noticeRechargeResult(info.pay_order_no);
        // 刷新会员卡信息
        await this._getCardInfo();
    },
    async _getCardInfo() {
        const accountInfo = await getMemberAccountInfo();
        console.log('Recharge => 获取的会员信息为：', accountInfo);
        if (!accountInfo) return;
        // 获取卡折扣
        const discountInfo = await getMemberCardDiscount(accountInfo.card_id);
        console.log('Recharge => 获取到的折扣信息', discountInfo);
        const cardInfo = {
            id: accountInfo.card_id,
            accId: accountInfo.acc_id,
            balance: accountInfo.balance,
            discount: discountInfo.discount / 10,
            cardName: accountInfo.card_name,
            integral: accountInfo.integral,
            integralUp: accountInfo.unit_integral_amount,
            rechargeUp: accountInfo.topup_limit,
            canRecharge: accountInfo.can_recharge,
            canIntegral: accountInfo.can_integral,
            cover: accountInfo.cover,
        };
        app.globalData.cardInfo = cardInfo;
        this.setData({
            cardInfo,
        });
    },
    gotoBalanceLog() {
        navTo(PAGE_NAV.balanceLog);
    },
    customValueChage(e) {
        console.log('Recharge => 自定义充值数值变化', e);
        const { value } = e.detail;
        this.setData({
            customValue: value,
            selectedKey: 0,
        });
    },
})